#include "fig_ui_language.h"
#include "fig_vars.h"
#include "fig_app.h"

#include "ui/ui_checkbox.h"
#include "ui/ui_manager.h"
#include "lang/strman.h"
#include "font/font_manager.h"
#include "font/font.h"



FIGUILanguage&	FIGUILanguage::Instance()
{
	static FIGUILanguage	Inst;
	return Inst;
}

FIGUILanguage::FIGUILanguage()
{
	
	

	m_pPopup = gameflow::UIPopupManager::Instance().GetPopup("ui/language.ui");

	if(m_pPopup)
	{
		m_pPopup->SetWidgetFunctor("ok_button",UI_DELEGATE(FIGUILanguage, OnButtonOk));		
	}


	const char* LanguageName = FIGVars::Instance()->GetLanguage();
	
	
	//if (0 == stricmp(szName, Name.c_str()))
	//	return true;

	float X = 0.1f;
	float Y = 0.1f;
	bool IsFound = false;

	std::string LanguageCompareStr;
	char	Buffer[256];
	for(int i = 0; i < MAX_LANGUAGE; i++)
	{
		sprintf(Buffer, "l_%i", i+ 1);
		SetupCallBackForButton(Buffer);

		if(m_pPopup)
		{
			ui::UIWidget* pWidget = m_pPopup->GetWidget(Buffer);
//#ifdef _PLATFORM_ANDROID
//			if( i > 4 && pWidget)
//			{
//				pWidget->SetIsVisible(false);
//			}
//#endif

			if(pWidget)
			{
				LanguageCompareStr = pWidget->GetOrigTextA();
				((ui::UICheckBox*)pWidget)->SetChecked(false);

				pWidget->SetPosition(math::Vec2(X,Y));
			}


			if( stricmp(LanguageCompareStr.c_str(),LanguageName) == 0)
			{
				((ui::UICheckBox*)pWidget)->SetChecked(true);
				IsFound = true;
			}
		}

		Y += 0.15f;
		if( i == (MAX_LANGUAGE / 2) - 1 )
		{
			X = 0.55f;
			Y = 0.1f;
		}
	}

	if(IsFound == false)
	{
		ui::UIWidget* pTempWidget = m_pPopup->GetWidget("l_1");
		if(pTempWidget)
		{
			((ui::UICheckBox*)pTempWidget)->SetChecked(true);
			FIGVars::Instance()->SetLanguage(pTempWidget->GetOrigTextA());	
			std::string Lang = "localization/" + pTempWidget->GetOrigTextA() + ".bin";
			lang::StrMan::Instance().LoadLangBin(Lang);
			ui::UIManager::Instance().RefreshText();
		}
	}
	
}


void				FIGUILanguage::SetupCallBackForButton(const std::string& ButtonName)
{
	
	if(m_pPopup)
		m_pPopup->SetWidgetFunctor(ButtonName,UI_DELEGATE(FIGUILanguage, OnButtonClick));
}

void DELEGATE_CALL	FIGUILanguage::OnButtonClick(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	
	char	Buffer[256];
	for(int i = 0 ; i < MAX_LANGUAGE; i++)
	{
		sprintf(Buffer, "l_%i", i+1);
		ui::UICheckBox* pChekbox = ((ui::UICheckBox*)(m_pPopup->GetWidget(Buffer)));
		if(pChekbox)
			pChekbox->SetChecked(false);
	}

	if(pWidget)
		((ui::UICheckBox*)pWidget)->SetChecked(true);

	if(pWidget->GetOrigTextA().compare(FIGVars::Instance()->GetLanguage()) == 0)
		return;

	FIGVars::Instance()->SetLanguage(pWidget->GetOrigTextA());	
	std::string Lang = "localization/" + pWidget->GetOrigTextA() + ".bin";
	lang::StrMan::Instance().LoadLangBin(Lang);

	if(pWidget->GetOrigTextA().compare("CHINESE") == 0 ||
		pWidget->GetOrigTextA().compare("KOREAN") == 0 ||
		pWidget->GetOrigTextA().compare("JAPANESE") == 0 )
	{
		//FIGApp::ChangeToUniCodeFont();
	}
	else
	{
		//FIGApp::ChangeToEnglishFont();
	}

	
	

    FontManager::Instance().Recreate();
	ui::UIManager::Instance().RefreshText();
	
    

}

void DELEGATE_CALL		FIGUILanguage::OnButtonOk(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	if(m_pPopup)
	{
		m_pPopup->GetWidget("ok_button")->SetIsVisible(false);	
		m_pPopup->GetWidget("Resume")->SetIsVisible(true);	
	}
	this->m_pPopup->Hide(NULL);
	FIGVars::Instance()->Save();
	int Test = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_LOCALIZATION_SELECTED);
	int test2 = 0;
}



